package com.hongkzh.www.look.view.activity;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.hongkzh.www.R;
import com.hongkzh.www.look.model.bean.AttenedBean;
import com.hongkzh.www.look.model.bean.HistoryBean;
import com.hongkzh.www.other.utils.SharedPreferencesUtil;
import com.hongkzh.www.other.utils.UIUtils;
import com.hongkzh.www.look.enterprise.presenter.HistoryPresenter;
import com.hongkzh.www.view.activity.BaseActivity;
import com.hongkzh.www.look.view.adapter.HistorySmlvAdapter;
import com.hongkzh.www.view.customview.MyFooter;
import com.hongkzh.www.look.view.iview.HistoryIView;
import com.liaoinstan.springview.widget.SpringView;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.liaoinstan.springview.utils.DensityUtil.dp2px;

/**
 * 历史记录的页面
 */
public class RecordActivity extends BaseActivity<HistoryPresenter> implements HistoryIView, SpringView.OnFreshListener,View.OnClickListener {

    @BindView(R.id.Btn_titleLeft)
    Button BtnTitleLeft;
    @BindView(R.id._title_left_container)
    RelativeLayout TitleLeftContainer;
    @BindView(R.id.Btn_titleMidContent)
    Button BtnTitleMidContent;
    @BindView(R.id.iv_titleIcon)
    ImageView ivTitleIcon;
    @BindView(R.id.title_bar_point)
    ImageView titleBarPoint;
    @BindView(R.id.history_smlv)
    SwipeMenuListView historySmlv;
    @BindView(R.id.springView_MyHistory)
    SpringView springViewMyHistory;

    private int pageNum=1;

    private HistorySmlvAdapter historySmlvAdapter;


    private boolean lastPage;
    private MyFooter myFooter;
    private String mLoginId;
    private SharedPreferencesUtil spfu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_record);
        ButterKnife.bind(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_record;
    }

    @Override
    protected void init() {

        spfu=new SharedPreferencesUtil(UIUtils.getContext());
        mLoginId=spfu.getUserInfo().getLoginUid();

        setPresenter(new HistoryPresenter());

        mTiManager.setTitle("历史观看");
        mTiManager.setLeftIcon(R.mipmap.arrow_left);

        myFooter=new MyFooter(this);
        springViewMyHistory.setFooter(myFooter);

        SwipeMenuCreator creator=new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                // create "open" item
                SwipeMenuItem openItem = new SwipeMenuItem(RecordActivity.this);
                // set item background
                openItem.setBackground(new ColorDrawable(Color.parseColor("#FF5252")));
                // set item width
                openItem.setWidth(dp2px(90));
                // set item title
                openItem.setTitle("删除");
                // set item title fontsize
                openItem.setTitleSize(12);
                // set item title font color
                openItem.setTitleColor(Color.WHITE);
                // add to menu
                menu.addMenuItem(openItem);
            }
        };

        historySmlvAdapter=new HistorySmlvAdapter();
        historySmlv.setAdapter(historySmlvAdapter);

        historySmlv.setMenuCreator(creator);
        historySmlv.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        //TODO: 取消历史记录的删除
                        getPresenter().getHistoryDelete(historySmlvAdapter.getBeanByPosition(position).getId());
                        historySmlvAdapter.remove(position);
                        historySmlvAdapter.notifyDataSetChanged();
                        break;
                }
                // false : close the menu; true : not close the menu
                return false;
            }
        });
        historySmlv.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT);

        getPresenter().getMyHistoryList(mLoginId,pageNum+"");

    }


    @Override
    protected void setListener() {

        mTiManager.getTitleLeft().setOnClickListener(this);
        springViewMyHistory.setListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.Btn_titleLeft:
                this.finish();
                break;
        }

    }

    @Override
    public void onShowHistoryList(HistoryBean historyBean) {

        if(pageNum==1){
            historySmlvAdapter.clearHistoryList();
        }
        historySmlvAdapter.add(historyBean);
        historySmlvAdapter.notifyDataSetChanged();
        springViewMyHistory.onFinishFreshAndLoad();

    }


    @Override
    public void deleteHistoryItem(AttenedBean dateBean) {

    }

    @Override
    public void onLastPage(boolean lastPage) {
        this.lastPage=lastPage;
        myFooter.setLastPage(lastPage);
    }

    @Override
    public void onError(Exception e) {

    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadmore() {
        if(lastPage){
            springViewMyHistory.onFinishFreshAndLoad();
        }else {
            pageNum++;
            getPresenter().getMyHistoryList(mLoginId,pageNum+"");
        }

    }
}
